|
1
|
|
|
import { fromJS, Map } from 'immutable'; |
|
2
|
|
|
import { generateLastUpdate } from './../../../util/lastUpdate'; |
|
3
|
|
|
|
|
4
|
|
|
import { |
|
5
|
|
|
getData, |
|
6
|
|
|
setDataAtDataIndex, |
|
7
|
|
|
setKeysInData |
|
8
|
|
|
} from './../../../util/getData'; |
|
9
|
|
|
|
|
10
|
|
|
export const editRow = (state, { |
|
11
|
|
|
columns, editMode, rowIndex, rowId, stateKey, top, isCreate, values |
|
12
|
|
|
}) => { |
|
13
|
|
|
|
|
14
|
|
|
const isValid = isRowValid(columns, values); |
|
15
|
|
|
const iOverrides = state.getIn([stateKey, rowId, 'overrides']) |
|
16
|
|
|
? state.getIn([stateKey, rowId, 'overrides']).toJS() |
|
17
|
|
|
: {}; |
|
18
|
|
|
|
|
19
|
|
|
columns.forEach((col, i) => { |
|
20
|
|
|
const val = getData(values, columns, i); |
|
21
|
|
|
const dataIndex = col.dataIndex; |
|
22
|
|
|
|
|
23
|
|
|
// setting disabled |
|
24
|
|
|
iOverrides[dataIndex] = iOverrides[dataIndex] || {}; |
|
25
|
|
|
iOverrides[dataIndex].disabled = setDisabled(col, val, values); |
|
26
|
|
|
}); |
|
27
|
|
|
|
|
28
|
|
|
const operation = editMode === 'inline' |
|
29
|
|
|
? 'setIn' |
|
30
|
|
|
: 'mergeIn'; |
|
31
|
|
|
|
|
32
|
|
|
return state[operation]([stateKey], fromJS({ |
|
33
|
|
|
[rowId]: { |
|
34
|
|
|
key: rowId, |
|
35
|
|
|
values, |
|
36
|
|
|
rowIndex, |
|
37
|
|
|
top, |
|
38
|
|
|
valid: isValid, |
|
39
|
|
|
isCreate: isCreate || false, |
|
40
|
|
|
overrides: iOverrides |
|
41
|
|
|
}, |
|
42
|
|
|
lastUpdate: generateLastUpdate() |
|
43
|
|
|
})); |
|
44
|
|
|
|
|
45
|
|
|
}; |
|
46
|
|
|
|
|
47
|
|
|
export const setData = (state, { data, editMode, stateKey }) => { |
|
48
|
|
|
if (editMode === 'grid') { |
|
49
|
|
|
|
|
50
|
|
|
const keyedData = setKeysInData(data); |
|
51
|
|
|
const editorData = keyedData.reduce((prev, curr, i) => { |
|
52
|
|
|
return prev.set(curr.get('_key'), fromJS({ |
|
53
|
|
|
key: curr.get('_key'), |
|
54
|
|
|
values: curr, |
|
55
|
|
|
rowIndex: i, |
|
56
|
|
|
top: null, |
|
57
|
|
|
valid: null, |
|
58
|
|
|
isCreate: false, |
|
59
|
|
|
overrides: {} |
|
60
|
|
|
})); |
|
61
|
|
|
}, Map({ lastUpdate: generateLastUpdate() })); |
|
62
|
|
|
|
|
63
|
|
|
return state.mergeIn([stateKey], editorData); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return state; |
|
67
|
|
|
}; |
|
68
|
|
|
|
|
69
|
|
|
export const rowValueChange = (state, { |
|
70
|
|
|
column, columns, value, rowId, stateKey |
|
71
|
|
|
}) => { |
|
72
|
|
|
|
|
73
|
|
|
const previousValues = state.getIn([stateKey, rowId, 'values']) |
|
74
|
|
|
? state.getIn([stateKey, rowId, 'values']).toJS() |
|
75
|
|
|
: {}; |
|
76
|
|
|
const overrides = state.getIn([stateKey, rowId, 'overrides']) |
|
77
|
|
|
? state.getIn([stateKey, rowId, 'overrides']).toJS() |
|
78
|
|
|
: {}; |
|
79
|
|
|
|
|
80
|
|
|
let rowValues = setDataAtDataIndex( |
|
81
|
|
|
previousValues, column.dataIndex, value |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
columns.forEach((col, i) => { |
|
85
|
|
|
const val = getData(rowValues, columns, i); |
|
86
|
|
|
const dataIndex = col.dataIndex; |
|
87
|
|
|
|
|
88
|
|
|
// interpreting `change func` to set final values |
|
89
|
|
|
// happens first, due to other validation |
|
90
|
|
|
rowValues = handleChangeFunc(col, rowValues); |
|
91
|
|
|
|
|
92
|
|
|
// setting default value |
|
93
|
|
|
if (col.defaultValue !== undefined |
|
94
|
|
|
&& val === undefined || val === null) { |
|
95
|
|
|
setDataAtDataIndex(rowValues, dataIndex, col.defaultValue); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// setting disabled |
|
99
|
|
|
overrides[dataIndex] = overrides[dataIndex] || {}; |
|
100
|
|
|
overrides[dataIndex].disabled = setDisabled(col, val, rowValues); |
|
101
|
|
|
|
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
const valid = isRowValid(columns, rowValues); |
|
105
|
|
|
|
|
106
|
|
|
state = state.mergeIn([stateKey, rowId], { |
|
107
|
|
|
values: rowValues, |
|
108
|
|
|
previousValues: state.getIn([stateKey, rowId, 'values']), |
|
109
|
|
|
valid, |
|
110
|
|
|
overrides |
|
111
|
|
|
}); |
|
112
|
|
|
|
|
113
|
|
|
return state.setIn( |
|
114
|
|
|
[stateKey, 'lastUpdate'], |
|
115
|
|
|
generateLastUpdate() |
|
116
|
|
|
); |
|
117
|
|
|
}; |
|
118
|
|
|
|
|
119
|
|
|
export const repositionEditor = (state, { rowId, stateKey, top }) => { |
|
120
|
|
|
const newState = state.mergeIn([stateKey, rowId], { |
|
121
|
|
|
top: top |
|
122
|
|
|
}); |
|
123
|
|
|
|
|
124
|
|
|
return newState.mergeIn( |
|
125
|
|
|
[stateKey], |
|
126
|
|
|
{ lastUpdate: generateLastUpdate() } |
|
127
|
|
|
); |
|
128
|
|
|
}; |
|
129
|
|
|
|
|
130
|
|
|
export const removeEditorState = (state, { stateKey }) => state.setIn( |
|
131
|
|
|
[stateKey], |
|
132
|
|
|
fromJS({ lastUpdate: generateLastUpdate() })); |
|
133
|
|
|
|
|
134
|
|
|
// helpers |
|
135
|
|
|
export const isCellValid = ({validator }, value, values) => { |
|
136
|
|
|
if (!validator || !typeof validator === 'function') { |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return validator({ value, values }); |
|
141
|
|
|
}; |
|
142
|
|
|
|
|
143
|
|
|
export const isRowValid = (columns, rowValues) => { |
|
144
|
|
|
for (let i = 0; i < columns.length; i++) { |
|
145
|
|
|
|
|
146
|
|
|
const col = columns[i]; |
|
147
|
|
|
const val = isCellValid(col, getData(rowValues, columns, i), rowValues); |
|
148
|
|
|
|
|
149
|
|
|
if (!val) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return true; |
|
155
|
|
|
}; |
|
156
|
|
|
|
|
157
|
|
|
export const setDisabled = (col = {}, value, values) => { |
|
158
|
|
|
|
|
159
|
|
|
if (col.disabled === true || col.disabled === 'false') { |
|
160
|
|
|
return col.disabled; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (typeof col.disabled === 'function') { |
|
164
|
|
|
return col.disabled({ column: col, value, values }); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return false; |
|
168
|
|
|
|
|
169
|
|
|
}; |
|
170
|
|
|
|
|
171
|
|
|
export const handleChangeFunc = (col, rowValues) => { |
|
172
|
|
|
|
|
173
|
|
|
if (!col.change || !typeof col.change === 'function') { |
|
174
|
|
|
return rowValues; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
const overrideValue = col.change({ values: rowValues }) || {}; |
|
178
|
|
|
|
|
179
|
|
|
Object.keys(overrideValue).forEach(k => { |
|
180
|
|
|
rowValues[k] = overrideValue[k]; |
|
181
|
|
|
}); |
|
182
|
|
|
|
|
183
|
|
|
return rowValues; |
|
184
|
|
|
}; |
|
185
|
|
|
|